package hn.cch.grammar.security;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64Util {

    // jdk1.6
    //  public static String encodeBuffer(byte[] bytes){
    //      BASE64Encoder base64Encoder = new BASE64Encoder();
    //      return base64Encoder.encodeBuffer(bytes);
    //  }
    //
    //  public static byte[] decodeBuffer(String string){
    //      BASE64Decoder base64Decoder = new BASE64Decoder();
    //      byte[] bytes = new byte[0];
    //      try {
    //          bytes = base64Decoder.decodeBuffer(string);
    //      } catch (IOException e) {
    //          System.out.println(e.getMessage());
    //      }
    //      return bytes;
    //  }


    // jdk1.8
    public static byte[] encode(byte[] bytes) {
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encode(bytes);
    }

    public static byte[] decode(byte[] bytes) {
        Base64.Decoder decoder = Base64.getDecoder();
        return decoder.decode(bytes);
    }
    public static void main(String[] args) {

        String data = "abcdef";
        String info = new String(Base64Util.encode(data.getBytes(StandardCharsets.UTF_8)));
        // String info = Base64Util.encodeBuffer(StringUtil.toBytes(data));
        System.out.println(info);

    }


}
